<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// 
//  user_model.php
//  www.6block.com
//  
//  Created by Will_Lee on 2011-05-21.
//  Copyright 2011 lishuzu. All rights reserved.
// 


class Notification_model extends CI_Model
{
		function __construct(){
			parent::__construct();
			$this->lang->load('feed');
			$this->user = $this->session->userdata('user') ? $this->session->userdata('user') : '';
		}
		
		
		
		// 得到uid 提醒
		function get_notices($uid, $num = '50', $offset = '', $new = '0'){
			$data = $notices =  array();
			if ($uid) {
				$offset = $offset ? $offset : 0;
				
				if ($new) {
					$this->db->where('new', '1');
				}
				
				$this->db->where('uid', $uid);
				$data['count'] = $this->db->count_all_results('notification');
				if ($data['count']) {
					if ($new) {
						$this->db->where('new', '1');
					}
					$this->db->where('uid', $uid);
					$this->db->order_by('dateline', 'DESC');
					$query = $this->db->get('notification', $num, $offset);
					foreach ($query->result_array() as $value) {
						$value['avatar_small'] = avatar($value['authorid'], 'small');
						$notices[] = $value;
					}
				}
				$data['list'] = $notices;
				if ($data) {
					return $data;
				}else{
					return false;
				}
			}
			return false;
		}
		
		//通知
		function notification_add($uid, $type, $note, $returnid=0) {
			//自己回复不产生提醒
			if ($uid == $this->user['uid']) {
				return false;
			}
			if ($uid && $type && $note) {
				$setarr = array(
					'uid' => $uid,
					'type' => $type,
					'new' => 1,
					'authorid' => $this->user['uid'],
					'author' => $this->user['name'],
					'note' => xss_clean($note),
					'dateline' => now()
				);

				//更新用户提醒
				$this->db->query("UPDATE ".$this->db->dbprefix('user')." SET notenum=notenum+1 WHERE uid='$uid'");
				$this->db->insert('notification', $setarr);
				//新提醒
				$this->db->query("REPLACE INTO ".$this->db->dbprefix."newnotice (uid) VALUES ('$uid')");	
				if($returnid) {
					return $this->db->insert_id();
				} else {
					return true;
				}
			}
			return false;
		}
		
		
		// 得到分页数据的mytalk
		function get_pagenotices($uid){
			if ($uid) {
				//page
				$_SGLOBAL = $this->config->item('SGLOBAL');
				$_SGLOBAL['multipage_config']['base_url'] = base_url().'notice/index/';
				$_SGLOBAL['multipage_config']['uri_segment'] = '3';
				$_SGLOBAL['multipage_config']['per_page'] = '50';//debug
				$offset = $this->uri->segment($_SGLOBAL['multipage_config']['uri_segment']);
				$data = $this->get_notices($uid, $_SGLOBAL['multipage_config']['per_page'], $offset);
				if ($data) {
					//page
					$_SGLOBAL['multipage_config']['total_rows'] = $data['count'];
					$this->pagination->initialize($_SGLOBAL['multipage_config']);
					$multi = $this->pagination->create_links();
					$data['multi'] = $multi;
					return $data;
				}else{
					return array();
				}
			}
			return false;
		}
		
		// 检查新提醒
		function check_newnotice($uid) {
				$newnotice = $this->db->query("SELECT count(*) as num FROM ".$this->db->dbprefix('newnotice')." WHERE uid='".$uid."'")->first_row()->num;
			return $newnotice;
		}
		
		function set_ignore($uid){
			if ($uid) {
				$this->db->update('notification', array('new'=>'0'), array('new'=>'1', 'uid'=>$uid));
				$this->db->update('user', array('notenum'=>0), array('uid'=>$uid));
				$this->db->query("DELETE FROM ".$this->db->dbprefix('newnotice')." WHERE uid='$uid'");
				return true;
			}
			return false;
		}
		
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
?>